﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplLogic.ApplClasses;
using ApplLogic.DbToApplConvertor;
using DAL;
using Entities;

namespace ApplLogic.ApplDbMediators
{
    public class ApplDbWorkerStatMediator
    {
        private DataService m_DataService = DataService.Instance;

        private static volatile ApplDbWorkerStatMediator s_Instance = null;
        private static object s_InstanceLockObj = new Object();

        private ApplDbWorkerStatMediator()
        {
        }

        public static ApplDbWorkerStatMediator Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    lock (s_InstanceLockObj)
                    {
                        if (s_Instance == null)
                        {
                            s_Instance = new ApplDbWorkerStatMediator();
                        }
                    }
                }

                return s_Instance;
            }
        }


        public void AddWorkerStat(ApplWorkerStatInWeek applWorkerStat, bool saveChanges = false)
        {
            m_DataService.AddWorkerStat(
                DbApplObjectConvertor.CreateDbWorkerStatInAWeekFromApplWorkerStatInAWeek(applWorkerStat), saveChanges);
        }

        public ApplWorkerStatInWeek GetWorkerStat(DateTime startTime, DateTime endTime, string workerId)
        {
            return
                DbApplObjectConvertor.CreateApplWorkerStatFromDbWorkerStat(m_DataService.GetWorkerStat(startTime,
                                                                                                       endTime, workerId));
        }

        public void DeleteStatInDateRangeForAllWorkers(DateTime startTime, DateTime endTime, bool saveChanges = false)
        {
            m_DataService.DeleteStatInDateRangeForAllWorkers(startTime, endTime, saveChanges);
        }

        public ApplWorkerStatInWeek GetAllWorkerStatOfWorker(string workerId)
        {
            ApplWorkerStatInWeek aggregatedWorkerStat = new ApplWorkerStatInWeek();
            aggregatedWorkerStat.WorkerRef = DbApplObjectConvertor.CreateApplWorkerFromDbWorker(m_DataService.GetWorkerByID(workerId));
            ApplWorkerStatInWeek minApplWorkerStat = DbApplObjectConvertor.CreateApplWorkerStatFromDbWorkerStat
                (m_DataService.GetMinimalWorkerStat(workerId));
            DateTime startTime = minApplWorkerStat.WeekStartTime;
            DateTime endTime = minApplWorkerStat.WeekSEndTime;
            bool toContinue = true;
            do
            {
                if (m_DataService.IsWorkerStatExist(startTime, endTime, workerId))
                {
                    ApplWorkerStatInWeek workerStatForWeek = DbApplObjectConvertor.CreateApplWorkerStatFromDbWorkerStat(m_DataService.GetWorkerStat(startTime, endTime, workerId));
                    aggregatedWorkerStat.ShiftsThatAskedAndGot += workerStatForWeek.ShiftsThatAskedAndGot;
                    aggregatedWorkerStat.ShiftsThatAskedNotToBeAndGot += workerStatForWeek.ShiftsThatAskedNotToBeAndGot;
                    aggregatedWorkerStat.ShiftsThatNotAskedAndGot += workerStatForWeek.ShiftsThatNotAskedAndGot;
                    aggregatedWorkerStat.TotalForbiddenShifts += workerStatForWeek.TotalForbiddenShifts;
                    aggregatedWorkerStat.TotalWantedShifts += workerStatForWeek.TotalWantedShifts;
                    aggregatedWorkerStat.TotalNumberOfShiftsGot += workerStatForWeek.TotalNumberOfShiftsGot;
                    startTime = startTime.AddDays(7);
                    endTime = endTime.AddDays(7);
                }
                else 
                {
                    if (startTime >= DateTime.Today)
                    {
                        aggregatedWorkerStat.WeekStartTime = minApplWorkerStat.WeekStartTime;
                        aggregatedWorkerStat.WeekSEndTime = endTime.AddDays(-7);
                        toContinue = false;
                    }
                    else
                    {
                        startTime = startTime.AddDays(7);
                        endTime = endTime.AddDays(7);
                    }
                }

            } while (toContinue);
            return aggregatedWorkerStat;
        }
    }
}
